package web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
* 基类
*
* 作用:调用它的子类的方法
* */
public class BaseServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Class clazz = this.getClass();
            System.out.println(clazz);
            String methodName = request.getParameter("methodName");
            System.out.println(methodName);
            if(methodName==null){
                methodName = "index" ;
            }
            Object obj = clazz.newInstance();
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(obj,request,response) ;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            throw new RuntimeException("程序反射异常...") ;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    public  void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        System.out.println("baseServlet的index执行了...");
    }

}
